package com.mt.order.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.common.constants.BaseErrorConstants;
import com.mt.common.util.ConvertUtil;
import com.mt.order.api.service.IOrderService;
import com.mt.order.entity.OrderDetailEntity;
import com.mt.order.entity.OrderEntity;
import com.mt.order.entity.OrderTradeEntity;
import com.mt.order.service.model.OrderDetailServiceModel;
import com.mt.order.service.model.OrderServiceModel;
import com.mt.order.service.model.OrderTradeServiceModel;
import org.springframework.stereotype.Service;

@Service
public class OrderServiceImpl implements IOrderService {

    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(OrderServiceImpl.class);

    @Autowired
    private OrderServiceModel orderServiceModel;

    @Autowired
    private OrderDetailServiceModel orderDetailServiceModel;

    @Autowired
    private OrderTradeServiceModel orderTradeServiceModel;

    public ServiceResult<OrderEntity> findInfo(Map<String, Object> params) {
        // 查询用户信息
        ServiceResult<OrderEntity> result = null;
        OrderEntity orderEntity = null;
        boolean withDetail = ConvertUtil.toBool(params.get("withDetail"), false);
        boolean withTrade = ConvertUtil.toBool(params.get("withTrade"), false);
        // 该参数标识前段是否需要权限数据
        try {
            result = orderServiceModel.selectInfo(params);
            orderEntity = result.getResult();
            if (orderEntity != null) {
                if (withDetail) {
                    ServiceResult<List<OrderDetailEntity>> orderDetailResult =
                        orderDetailServiceModel.selectList(params, null);
                    orderEntity.setOrderDetailList(orderDetailResult.getResult());
                }
                if (withTrade) {
                    ServiceResult<List<OrderTradeEntity>> orderTradeResult =
                        orderTradeServiceModel.selectList(params, null);
                    orderEntity.setOrderTradeList(orderTradeResult.getResult());
                }

            }
        } catch (Exception e) {
            LOGGER.error("[OrderServiceImpl][findInfo]：query findInfo occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询详情错误");
        }
        return result;
    }

    public ServiceResult<List<OrderEntity>> findList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        ServiceResult<List<OrderEntity>> result = new ServiceResult<List<OrderEntity>>();
        boolean withDetail = ConvertUtil.toBool(params.get("withDetail"), false);
        try {
            result = orderServiceModel.selectList(params, pagerInfo);
            if (withDetail) {
                List<OrderEntity> orderList = result.getResult();
                if (orderList != null && orderList.size() > 0) {
                    Map<String, Object> detailParams = new HashMap<String, Object>();
                    for (OrderEntity entity : orderList) {
                        detailParams.put("orderId", entity.getOrderId());
                        ServiceResult<List<OrderDetailEntity>> orderDetailResult =
                            orderDetailServiceModel.selectList(detailParams, null);
                        entity.setOrderDetailList(orderDetailResult.getResult());
                    }
                    result.setResult(orderList);
                }
            }
        } catch (Exception e) {
            LOGGER.error("[OrderServiceImpl][findList]：query findList occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "查询列表错误");
        }

        return result;
    }

    public ServiceResult<Integer> doInsert(OrderEntity orderEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = orderServiceModel.insert(orderEntity);
        } catch (Exception e) {
            LOGGER.error("[OrderServiceImpl][doInsert]：query doInsert occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "创建信息错误");
        }
        return result;
    }

    public ServiceResult<List<OrderEntity>> doInsertList(List<OrderEntity> orderEntityList) {
        ServiceResult<List<OrderEntity>> result = new ServiceResult<List<OrderEntity>>();
        try {
            result = orderServiceModel.insertList(orderEntityList);
        } catch (Exception e) {
            LOGGER.error("[OrderServiceImpl][doInsert]：query doInsert occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "创建信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdateRelate(OrderEntity orderEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = orderServiceModel.updateRelate(orderEntity);
        } catch (Exception e) {
            LOGGER.error("[OrderServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "编辑信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdatePaymentInfo(OrderEntity orderEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = orderServiceModel.doUpdatePaymentInfo(orderEntity);
        } catch (Exception e) {
            LOGGER.error("[OrderServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "编辑信息错误");
        }
        return result;
    }

    public ServiceResult<Integer> doDeleteById(Integer orderId) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = orderServiceModel.deleteByPrimaryKey(orderId);
        } catch (Exception e) {
            LOGGER.error("[OrderServiceImpl][doDeleteById]：query doDeleteById occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "删除信息错误");
        }
        return result;
    }

    @Override
    public ServiceResult<Integer> doUpdateList(Map<String, Object> params) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = orderServiceModel.updateList(params);
        } catch (Exception e) {
            LOGGER.error("[OrderServiceImpl][doUpdateList]：query doUpdateList occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "批量更新");
        }
        return result;
    }

    public ServiceResult<Integer> doUpdate(OrderEntity orderEntity) {
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        try {
            result = orderServiceModel.updateByPrimaryKeySelective(orderEntity);
        } catch (Exception e) {
            LOGGER.error("[OrderServiceImpl][doUpdate]：query doUpdate occur exception", e);
            result.setError(BaseErrorConstants.ERROR_CODE_SERVICE_CONTROLLER, "更新信息错误.");
        }
        return result;
    }

}
